//----------------------------------------HUD
surface.CreateFont("coolvetica", 28, 500, true, false, "HUD_Font")
local fade_offset = 0 //needs to keep its value outside of DrawHUD

function DrawHUD()
	//DEFINES
	local TOP_BAR_SIZE = 20
	local TOP_BAR_BORDER_SIZE = 3
	local SCORE_BOX_WIDTH = 200
	local SCORE_BOX_HEIGHT = 82
	local SCORE_BOX_CORNER_SIZE = 10

	local TOP_BAR_COLOR = Color(64, 64, 64, 180)
	local TOP_BAR_BORDER_COLOR = Color(0, 0, 0, 255)
	//local TOP_BAR_BORDER_COLOR2 = Color(255, 255, 0, 255)
	local TOP_BAR_BORDER_COLOR2 = team.GetColor(LocalPlayer():Team())
	local TOP_BAR_TEXT_COLOR = Color(255, 255, 255, 255)
	local SCORE_BOX_COLOR = TOP_BAR_COLOR //I wouldn't change this...

	local TOP_BAR_FONT = "HudHintTextLarge"
	local SCORE_BOX_FONT = "HUD_Font"

	local FADE_RATE = 0.5
	local FADE_ACCURACY = 40


	//bar background
	draw.RoundedBox( 0, 0, 0, ScrW()/2 - SCORE_BOX_WIDTH/2, TOP_BAR_SIZE, TOP_BAR_COLOR );
	draw.RoundedBox( 0, ScrW()/2 + SCORE_BOX_WIDTH/2, 0, ScrW()/2 - SCORE_BOX_WIDTH/2, TOP_BAR_SIZE, TOP_BAR_COLOR );

	//score panel
	draw.RoundedBox( SCORE_BOX_CORNER_SIZE, ScrW()/2 - SCORE_BOX_WIDTH/2, -25, SCORE_BOX_WIDTH, SCORE_BOX_HEIGHT + TOP_BAR_SIZE + TOP_BAR_BORDER_SIZE, SCORE_BOX_COLOR );

	//bar border
	local num_chunks = FADE_ACCURACY * 2 + 3 //3 for left center and right
	local chunk_size = ScrW() / num_chunks
	local r_unit = (TOP_BAR_BORDER_COLOR2.r - TOP_BAR_BORDER_COLOR.r) / (FADE_ACCURACY + 1)
	local g_unit = (TOP_BAR_BORDER_COLOR2.g - TOP_BAR_BORDER_COLOR.g) / (FADE_ACCURACY + 1)
	local b_unit = (TOP_BAR_BORDER_COLOR2.b - TOP_BAR_BORDER_COLOR.b) / (FADE_ACCURACY + 1)
	local a_unit = (TOP_BAR_BORDER_COLOR2.a - TOP_BAR_BORDER_COLOR.a) / (FADE_ACCURACY + 1)

	fade_offset = fade_offset + FADE_RATE
	if fade_offset > num_chunks then
		fade_offset = 0
	end

	for i=1, num_chunks do
		local position = (i-1 + math.floor(fade_offset)) * chunk_size

		while (position >= ScrW()) do
			position = position - ScrW()
		end

		local color_position

		if i > math.ceil(num_chunks / 2) then
			color_position = math.ceil(num_chunks / 2) - (i - math.ceil(num_chunks / 2))
		else
			color_position = i - 1
		end

		local new_color = Color(
			TOP_BAR_BORDER_COLOR.r + (r_unit * color_position),
			TOP_BAR_BORDER_COLOR.g + (g_unit * color_position),
			TOP_BAR_BORDER_COLOR.b + (b_unit * color_position),
			TOP_BAR_BORDER_COLOR.a + (a_unit * color_position) )

		draw.RoundedBox( 0, position, TOP_BAR_SIZE, chunk_size + 1, TOP_BAR_BORDER_SIZE, new_color );
		//I add one to the size to combat a rounding error you sometimes get
	end

	//bar text
	local round = ""
	if gb_CurrentRound == 1 then
		round = "Build"
	elseif gb_CurrentRound == 2 then
		round = "Fight"
	else
		round = "Post Game"
	end
	local spacing = (ScrW()-4) / 5
	draw.DrawText( "Core Health: "..LocalPlayer():GetNetworkedInt("gb_corehealth"), TOP_BAR_FONT, 2 + (spacing * 0), 2, TOP_BAR_TEXT_COLOR)
	draw.DrawText( "Time Left: "..string.FormattedTime( gb_RoundTimer, "%02i:%02i"), TOP_BAR_FONT, 2 + (spacing * 0.7), 2, TOP_BAR_TEXT_COLOR)
	draw.DrawText( "Round: "..round, TOP_BAR_FONT, 2 + (spacing * 4.4), 2, TOP_BAR_TEXT_COLOR)

	//score panel text
	draw.DrawText( "Cores Remaining", SCORE_BOX_FONT, ScrW()/2 - 82, TOP_BAR_SIZE + TOP_BAR_BORDER_SIZE + 2, Color(255, 255, 255, 255))
	draw.DrawText( "Red: "..gb_NumRedCores, SCORE_BOX_FONT, ScrW()/2 - 90, TOP_BAR_SIZE + TOP_BAR_BORDER_SIZE + 2 + 27, Color(255, 0, 0, 255))
	draw.DrawText( "|", SCORE_BOX_FONT, ScrW()/2 - 3, TOP_BAR_SIZE + TOP_BAR_BORDER_SIZE + 2 + 27, Color(0, 0, 0, 255))
	draw.DrawText( "Blue: "..gb_NumBlueCores, SCORE_BOX_FONT, ScrW()/2 + 20, TOP_BAR_SIZE + TOP_BAR_BORDER_SIZE + 2 + 27, Color(0, 0, 255, 255))
end
hook.Add("HUDPaint", "DrawHUD", DrawHUD); 

function HideHud(name)      
	for k, v in pairs{"CHudHealth", "CHudBattery", "CHudAmmo", "CHudSecondaryAmmo"} do          
		if name == v then 
			return false      
		end        
	return true   
	end  
end
hook.Add("HUDShouldDraw", "Hide HUD Components", HideHud)

//----------------------------------------GUI
//VGUI DEFINES
local WINDOW0 = {}

function WINDOW0:Init()
	self.HEIGHT = 400
	self.WIDTH = 250
	self.CENTER_OFFSET = 150
	self.DROP_AMOUNT = 100
	self.CORNER_SIZE = 10

	self.MAIN_COLOR = Color(64, 64, 64, 255)

	self.SIZE_EXTENTION = 10

	self.OpenPos = self.DROP_AMOUNT
	self.ClosedPos = -self.HEIGHT - self.SIZE_EXTENTION + 1

	self:SetZPos(1)
	self:SetSize(self.WIDTH, self.HEIGHT + self.SIZE_EXTENTION)
	self:SetPos(0, self.ClosedPos)
	self.open = false
	self:SetVisible(true)
end

function WINDOW0:SetHeight()
	self:SetSize(self.WIDTH, self.HEIGHT + self.SIZE_EXTENTION)
	self.ClosedPos = -self.HEIGHT - self.SIZE_EXTENTION + 1
end

function WINDOW0:Paint()
	if self.open then
		if self.X != self.OpenPos then
			self:SetPos(self.X, math.Approach( self.Y, self.OpenPos, (math.abs( self.Y - self.OpenPos ) + 1) * 10 * FrameTime() ))
		end
	else
		if self.X != self.ClosedPos then
			self:SetPos(self.X, math.Approach( self.Y, self.ClosedPos, (math.abs( self.Y - self.ClosedPos ) + 1) * 10 * FrameTime() ))
		end
	end

	draw.RoundedBox( self.CORNER_SIZE, 0, 0, self:GetWide(), self:GetTall() - self.SIZE_EXTENTION, self.MAIN_COLOR )
	return true
end
vgui.Register("BaseMenu", WINDOW0)

local WINDOW1 = {}

function WINDOW1:Init()
	//BUTTONS and TITLES
	local offset = 5
	local title1 = vgui.Create( "MenuTitle", self)
	title1.TEXT = "Control Panel"
	title1:SetPos(5, offset)
	offset = offset + 30

	//local button1 = vgui.Create( "MenuButton", self)
	//button1.TEXT = "Toggle Camera"
	//button1.COMMAND = "gb_togglecam"
	//button1:SetPos(5, offset)
	//offset = offset + 30

	local button2 = vgui.Create( "MenuButton", self)
	button2.TEXT = "Forfeit"
	button2.COMMAND = "gb_forfeit"
	button2:SetPos(5, offset)
	offset = offset + 30

	self.HEIGHT = offset
	self:SetHeight()
	self:SetPos(ScrW()/2 - self.CENTER_OFFSET - self.WIDTH, self.ClosedPos)
end
vgui.Register("F3Menu", WINDOW1, "BaseMenu")

local WINDOW2 = {}

function WINDOW2:Init()

	//BUTTONS and TITLES
	local offset = 5
	local title1 = vgui.Create( "MenuTitle", self)
	title1.TEXT = "SENTS"
	title1:SetPos(5, offset)
	offset = offset + 30

	local button1 = vgui.Create( "MenuButton", self)
	button1.TEXT = "Spawn Core"
	button1.COMMAND = "gm_spawnsent gb_core"
	button1:SetPos(5, offset)
	offset = offset + 30

	local button2 = vgui.Create( "MenuButton", self)
	button2.TEXT = "Spawn Camera"
	button2.COMMAND = "gm_spawnsent gb_cam"
	button2:SetPos(5, offset)
	offset = offset + 30

	self.HEIGHT = offset
	self:SetHeight()
	self:SetPos(ScrW()/2 + self.CENTER_OFFSET, self.ClosedPos)
end
vgui.Register("F4Menu", WINDOW2, "BaseMenu")

local BUTTON = {}

function BUTTON:Init()
	self.WIDTH = 240
	self.HEIGHT = 25
	self.CORNER_SIZE = 10

	self.TEXT_XOFFSET = 3
	self.TEXT_YOFFSET = 5

	self.COLOR = Color(128, 128, 128, 255)
	self.HOVER_COLOR = Color(64, 64, 255, 255)
	self.PUSH_COLOR = Color(255, 255, 64, 255)
	self.TEXT_COLOR = Color(255, 255, 255, 255)

	self.TEXT = "Default Text"
	self.FONT = "HudHintTextLarge"

	self.COMMAND = "say lulz"

	self.color = self.COLOR

	self:SetZPos(2)
	self:SetSize(self.WIDTH, self.HEIGHT) //default
end

function BUTTON:DoClick()
	surface.PlaySound( "ui/buttonclick.wav" )
	LocalPlayer():ConCommand(self.COMMAND)
end

function BUTTON:Paint()
	if self.Selected then
		self.color = self.PUSH_COLOR
	elseif self.Armed then
		self.color = self.HOVER_COLOR
	else
		self.color = self.COLOR
	end

	draw.RoundedBox( self.CORNER_SIZE, 0, 0, self:GetWide(), self:GetTall(), self.color )
	draw.DrawText( self.TEXT, self.FONT, self.TEXT_XOFFSET, self.TEXT_YOFFSET, self.TEXT_COLOR )
	return true
end
vgui.Register( "MenuButton", BUTTON, "Button" )

local TITLELABEL = {}

function TITLELABEL:Init()
	self.WIDTH = 240
	self.HEIGHT = 25
	self.CORNER_SIZE = 10

	self.TEXT_XOFFSET = 3
	self.TEXT_YOFFSET = 5

	self.COLOR = Color(255, 0, 0, 255)
	self.TEXT_COLOR = Color(255, 255, 255, 255)

	self.TEXT = "Default Text"
	self.FONT = "HudHintTextLarge"

	self:SetZPos(2)
	self:SetSize(self.WIDTH, self.HEIGHT)
end

function TITLELABEL:Paint()
	draw.RoundedBox( self.CORNER_SIZE, 0, 0, self:GetWide(), self:GetTall(), self.COLOR )
	draw.DrawText( self.TEXT, self.FONT, self.TEXT_XOFFSET, self.TEXT_YOFFSET, self.TEXT_COLOR )
	return true
end
vgui.Register( "MenuTitle", TITLELABEL )

F1Menu = vgui.Create( "F3Menu" )
F2Menu = vgui.Create( "F3Menu" )
F3Menu = vgui.Create( "F3Menu" )
F4Menu = vgui.Create( "F4Menu" )

//MENU CONRTOL
function ShowF1Menu( um )
	if GAMEMODE.ShowScoreboard then return end

	F1Menu.open = !F1Menu.open
	if (!F1Menu.open && !F2Menu.open && !F3Menu.open && !F4Menu.open) then
		gui.EnableScreenClicker( false )
	else
		gui.EnableScreenClicker( true )
	end
end
//usermessage.Hook("ShowF1Menu", ShowF1Menu)

function ShowF2Menu( um )
	if GAMEMODE.ShowScoreboard then return end

	F2Menu.open = !F2Menu.open
	if (!F1Menu.open && !F2Menu.open && !F3Menu.open && !F4Menu.open) then
		gui.EnableScreenClicker( false )
	else
		gui.EnableScreenClicker( true )
	end
end
//usermessage.Hook("ShowF2Menu", ShowF2Menu)

function ShowF3Menu( um )
	if GAMEMODE.ShowScoreboard then return end

	F3Menu.open = !F3Menu.open
	if (!F1Menu.open && !F2Menu.open && !F3Menu.open && !F4Menu.open) then
		gui.EnableScreenClicker( false )
	else
		gui.EnableScreenClicker( true )
	end
end
usermessage.Hook("ShowF3Menu", ShowF3Menu)

function ShowF4Menu( um )
	if GAMEMODE.ShowScoreboard then return end

	F4Menu.open = !F4Menu.open
	if (!F1Menu.open && !F2Menu.open && !F3Menu.open && !F4Menu.open) then
		gui.EnableScreenClicker( false )
	else
		gui.EnableScreenClicker( true )
	end
end
usermessage.Hook("ShowF4Menu", ShowF4Menu)
